package com.hua.nailcampus.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.nailcampus.acl.mapper.AvatarMapper;
import com.hua.nailcampus.acl.mapper.FriendsMapper;
import com.hua.nailcampus.acl.mapper.UserMapper;
import com.hua.nailcampus.acl.model.Avatar;
import com.hua.nailcampus.acl.model.Friends;
import com.hua.nailcampus.acl.model.User;
import com.hua.nailcampus.acl.model.vo.FriendsVo;
import com.hua.nailcampus.acl.model.vo.UserVo;
import com.hua.nailcampus.acl.service.FriendsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:chenjianhua
 * @Date:2023/11/15 20:00
 * @Version 1.8
 */
@Slf4j
@Service
public class FriendsServiceImpl extends ServiceImpl<FriendsMapper, Friends>
    implements FriendsService {

    @Autowired
    private FriendsMapper friendsMapper;

    @Autowired
    private AvatarMapper avatarMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean removeFriendById(Integer userId, Integer fid) {
//        删除自己跟好友的数据
        int count = friendsMapper.delete(
                new LambdaQueryWrapper<Friends>()
                        .eq(Friends::getUId, userId)
                        .eq(Friends::getFId, fid));
//        删除好友跟自己的数据
        friendsMapper.delete(
                new LambdaQueryWrapper<Friends>()
                        .eq(Friends::getFId, userId)
                        .eq(Friends::getUId, fid));
        if(count != 1){
            return false;
        }
        return true;
    }
    @Override
    public List<UserVo> getFriendListById(Integer userId) {
        LambdaQueryWrapper<Friends> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Friends::getUId, userId).eq(Friends::getStatus,1);
        List<UserVo> userVoList = new ArrayList<UserVo>();
        //获取自己所有好友关系
        List<Friends> friendsList = friendsMapper.selectList(queryWrapper);
        //获取所有好友Id
        for (Friends friend : friendsList) {
//            根据好友Id获取用户信息
            User user = userMapper.selectById(friend.getFId());
//            根据好友Id获取头像
            Avatar avatar = avatarMapper.selectOne(
                    new LambdaQueryWrapper<Avatar>()
                            .eq(Avatar::getUserId,friend.getFId()));
            UserVo userVo = new UserVo(user);
            if(friend.getFName()!=null){
                userVo.setUserName(friend.getFName());
            }
            if(avatar!=null){
                userVo.setAvaUrl(avatar.getUrl());
            }
            userVoList.add(userVo);
        }
        return userVoList;
    }

    @Override
    public Boolean getRelationship(Integer uid, Integer fid) {
//        根据自己ID与用户ID查询是否是好友关系
        Friends friends = friendsMapper.selectOne(
                new LambdaQueryWrapper<Friends>()
                        .eq(Friends::getUId, uid)
                        .eq(Friends::getFId, fid)
                        .eq(Friends::getStatus,1));

//        查询到null表示还不是好友关系
        if(friends == null){
            return false;
        }
        return true;
    }

    @Override
    public void addFriend(Friends friend) {
        log.info(friend.getUId() + ": " + friend.getFId());
//        如果两个ID相同，则为自己跟自己，不用通过验证，直接就设置为好友关系
        if(friend.getFId().equals(friend.getUId())){
            friend.setStatus(1);
        }
//               存入数据库
        friend.setRemarksId(friend.getUId());
        friendsMapper.insert(friend);
//        如果是两个用户，则再交换ID，再存一次
        if(!friend.getFId().equals(friend.getUId())){
            Friends friends1 = new Friends();
            friends1.setFId(friend.getUId());
            friends1.setUId(friend.getFId());
            friends1.setRemarks(friend.getRemarks());
            friends1.setRemarksId(friend.getUId());
            friendsMapper.insert(friends1);
        }
    }

    @Override
    public void passAdd( Friends friend) {
        friend.setStatus(1);
        log.info("friend:{}",friend);
//                更新到数据库
        friendsMapper.updateById(friend);
        Friends friends = friendsMapper.selectOne(
                new LambdaQueryWrapper<Friends>()
                        .eq(Friends::getUId, friend.getFId())
                        .eq(Friends::getFId, friend.getUId()));
        friends.setStatus(1);
        log.info("friends:{}",friends);
        friendsMapper.updateById(friends);
    }

    @Override
    public List<FriendsVo> getFriendAddMessage(Integer id) {
//        获取所有申请好友用户的ID
        List<Friends> friendsList = friendsMapper.selectList(
                new LambdaQueryWrapper<Friends>()
                        .eq(Friends::getUId, id)
                        .eq(Friends::getStatus, 0)
                        );
//        创建UserVo集合
        List<FriendsVo> friendsVoList = new ArrayList<FriendsVo>();
//        创建UserVo对象
        FriendsVo friendsVo;
//        遍历好友关系列表集合取得每个用户的ID
        for (Friends friend : friendsList){
            if (friend.getRemarksId().equals(id)){
                continue;
            }
//            根据获取的ID查询用户实体类
            User user = userMapper.selectById(friend.getFId());
//            根据获取的ID查询用户的头像地址
            Avatar avatar = avatarMapper.selectOne(
                    new LambdaQueryWrapper<Avatar>()
                            .eq(Avatar::getUserId, friend.getFId()));
//            初始化friendsVo
            friendsVo = new FriendsVo(friend);
            friendsVo.setUserName(user.getUserName());
//            如果查询的头像实体类不为空，则将其放入friendsVo中
            if(avatar != null){
                friendsVo.setAvaUrl(avatar.getUrl());
            }
//            将friendsVo存入集合中
            friendsVoList.add(friendsVo);
        }
//        返回friendsVoList集合
        return friendsVoList;
    }

    @Override
    public Friends getaddFriends(Integer userId, Integer fid) {
        Friends friends = friendsMapper.selectOne(
                new LambdaQueryWrapper<Friends>()
                        .eq(Friends::getUId, userId)
                        .eq(Friends::getFId, fid));
        return friends;
    }

    @Override
    public void updateNickName(Integer userId, Integer fId, String nickName) {
        Friends friends = friendsMapper.selectOne(new LambdaQueryWrapper<Friends>()
                .eq(Friends::getUId, userId)
                .eq(Friends::getFId, fId));
        friends.setFName(nickName);
        friendsMapper.updateById(friends);
    }
}
